package com.xju.atpgs.config;

import com.xju.atpgs.security.AdminSuccessHandler;
import com.xju.atpgs.service.Impl.UsersDetailServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private AdminSuccessHandler successHandler;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable();
//                ignoringAntMatchers("/druid/*","/question/*","/exam/*");
        http.authorizeRequests()
                .antMatchers("/login","/loginForm").permitAll()
                .antMatchers("/gentelella/**","/images/**","/images/favicon.ico").permitAll()
                .anyRequest().authenticated();
        http.formLogin().loginPage("/login").loginProcessingUrl("/loginForm").usernameParameter("Username").passwordParameter("Password").successHandler(successHandler);
        http.logout();
        http.rememberMe().rememberMeParameter("remember");
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
//        auth.inMemoryAuthentication().passwordEncoder(passwordEncoder()).withUser("admin").password(passwordEncoder().encode("123")).roles("VIP");
        auth.userDetailsService(userDetailsService()).passwordEncoder(passwordEncoder());
    }

    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }

    @Bean
    public UsersDetailServiceImpl userDetailsService(){
        return new UsersDetailServiceImpl();
    }
}
